const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const BluetoothLockDevice = sequelize.define('BluetoothLockDevice', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  lockId: {
    type: DataTypes.STRING,
    allowNull: false
  },
  username: {
    type: DataTypes.STRING,
    allowNull: true
  },
  deviceId: {
    type: DataTypes.STRING,
    allowNull: true
  },
  operatingTime: {
    type: DataTypes.DATE,
    allowNull: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: true
  },
  model: {
    type: DataTypes.STRING,
    allowNull: true
  },
  department: {
    type: DataTypes.STRING,
    allowNull: true
  },
  lockStatus: {
    type: DataTypes.STRING,
    allowNull: true
  },
  active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  },
  lng: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  lat: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  position: {
    type: DataTypes.STRING,
    allowNull: false
  },
  remark: {
    type: DataTypes.STRING,
    allowNull: true
  },
  opDate: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  },
  alias: {
    type: DataTypes.STRING,
    allowNull: true
  },
  oName: {
    type: DataTypes.STRING,
    allowNull: true
  }
});

// 同步模型到数据库
BluetoothLockDevice.sync({ alter: true }).then(() => {
  console.log('BluetoothLockDevice table has been updated');
}).catch(err => {
  console.error('Error updating BluetoothLockDevice table:', err);
});

module.exports = BluetoothLockDevice; 